package com.zyh.cxf;


import com.zyh.dao.KeyMapper;
import com.zyh.util.RandomNumber;
import com.zyh.util.safeutil.DESedeUtil;
import com.zyh.util.safeutil.SM2Utils;
import com.zyh.util.safeutil.Util;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Map;

@Controller
public class MainClientCxfCYF {
  /*  @Autowired
    private KeyMapper keyMapper  ;
*/

    @Test
    public void aa()throws Exception{
        String data = "OyBBzjRiQ++xD9dqGa2subhRmh52I8RVf26mkSBQrWmOqdeOxP6OGiBwgfk3T7pWoiNRTh8qLaVg1kFspJNmLVxk1v1mly/DvBDC0OYPedP4+lmBrdZ6SE2ztaFUnuaiBQ0ldcX/6pJMh6WThAtA469zli2g3f8vnL1linQcqwTN2tDQBh+/gcnLCJP+Yby6TXi1meceBpqe0m+uCUs2OFg/t0y0ALlmdhyMc1Qy06L+eVqc2litdciI1chobn3q3mV/ueknhmJ/tbg1oxuDNane2dv/HQsc/3zXtmh9C3u4Jx9sNnkoKwDOOX78WH0ah0iiqHR/4bx0BM0KQNVPj5s6V0jfJpVegtai2773d3G1DU88VpeiKLTMPVUzziHl79JzH5yyUbdYP7dMtAC5ZifAgKpntXHNGhaBAqtWyB4/C7cl7kKuY5wdd3uZq9xKt++VJ4LDPa+gZ7J9LAwcdgONhAwwCsoFALQy+3bCP/6Azx1n2qG60ASTjjoVRkBv1+H0hXzr2Lqei6w8Xu4YwjoZtsxOX/c0ucZJsb+BfbTB2TC1aTil37Ee0t5nt7YuoGeyfSwMHHZKXcq+leOjz2VW5Q2QYT+xNyQmVaiuWbF7XDE+tmaQda5tytCbzZnee8M+QXn0U96sZ43OQubuHHyKbJsu09+K77TqZa85U2oznmae/gvVh2ZuZV1IdpVzFsOb1yua4q+oKxo4Ky5my6n61pn7CNXOe8S4NwJ4DS5SH6L2evPvjSTumQ8nzZ44JuEYrwOX3TM= ";
        System.out.println(DESedeUtil.decrypt(data, "dvG5s0k4v7Y0Z8ut+HmDZJeJXvRtaIWY"));
    }



    //交易结果查询
    //@Test
//    @RequestMapping("test1")
//    @ResponseBody
    @Test
    public void TestgetFundPayOrderResult(){

        JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxWsProxyFactoryBean();

        String URL = "http://localhost:8080/ssm/webservices/getFundPayResult";
//        String URL = "http://10.229.1.149:8091/ssm/webservices/getFundPayResult";
        jaxWsProxyFactoryBean.setAddress(URL);
        jaxWsProxyFactoryBean.setServiceClass(FundPayOrderResult.class);
        FundPayOrderResult fundPayOrderResult  = (FundPayOrderResult) jaxWsProxyFactoryBean.create();

        //获取请求参数
        Document doc = DocumentHelper.createDocument();
        Element Message = doc.addElement("Message");
        Element Head = Message.addElement("Head");
        Element Body = Message.addElement("Body");
        Element serialNo = Head.addElement("serialNo");
        Element reqTime = Head.addElement("reqTime");

        serialNo.setText("789");
        reqTime.setText("20080924235925");

        Element orderFlowNo=Body.addElement("orderFlowNo");

        orderFlowNo.setText("1");//医保端传的企业流水号
     /*   Map<String, Object> keyMap = keyMapper.querykey();
        //加密的公钥
        String inencryptKey=(String)keyMap.get("PublicKey");
        String privateKey=(String)keyMap.get("PrivateKey");
        //第一步密钥解密
        System.err.println("密钥sm2私钥解密：");
        String plainText = null;
        try {
            plainText = new String(SM2Utils.decrypt(Util.hexToByte(privateKey), Util.hexToByte(inencryptKey)));
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("密钥明文：" + plainText);

        // 加密
        String inencryptStr = null;

        try {
            inencryptStr = DESedeUtil.encrypt(doc.asXML(), plainText);
        } catch (Exception e) {
            e.printStackTrace();
        }

        System.out.println("医保端密文："+ inencryptStr);
*/

        String inencryptStr = null;
        try {
            inencryptStr = DESedeUtil.encrypt(doc.asXML(), "4+oOg7N/0IwWa5uk41HlHINPXhAZByVd");
        } catch (Exception e) {
            e.printStackTrace();
        }
        String  xmlData= fundPayOrderResult.getFundPayResult(inencryptStr);


        System.out.println("=====jjj===="+xmlData);
    }

    //用来获取sm4密钥的方法
    @Test
    public void TestgetSm4Key(){

        JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxWsProxyFactoryBean();

        String URL = "http://localhost:8080/ssm/webservices/getPublicKeyInfo";
        jaxWsProxyFactoryBean.setAddress(URL);
        jaxWsProxyFactoryBean.setServiceClass(GetPublicKey.class);
        GetPublicKey getKey  = (GetPublicKey) jaxWsProxyFactoryBean.create();
        //发送webservice
        String  xmlData= getKey.getPublicKeyInfo("66666");
        System.out.println(xmlData);
    }


    public String sendHttpPost(String wsdl, String xml)  {

        int timeout = 10000;
        // HttpClient发送SOAP请求
        System.out.println("HttpClient 发送SOAP请求");
        HttpClient client = new HttpClient();
        PostMethod postMethod = new PostMethod(wsdl);
        // 设置连接超时
        client.getHttpConnectionManager().getParams().setConnectionTimeout(timeout);
        // 设置读取时间超时
        client.getHttpConnectionManager().getParams().setSoTimeout(timeout);
        // 然后把Soap请求数据添加到PostMethod中
        RequestEntity requestEntity = null;
        try {
            requestEntity = new StringRequestEntity(xml, "text/xml", "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        // 设置请求体
        postMethod.setRequestEntity(requestEntity);
        int status = 0;
        try {
            status = client.executeMethod(postMethod);
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 打印请求状态码



        // 获取响应体输入流
        String result = null;
        try {
            result = postMethod.getResponseBodyAsString();

        } catch (IOException e) {
            e.printStackTrace();
        }
        // 获取请求结果字符串
        return result;

    }



    //拼接soapXml请求
    public static String getSoapXml( String methodName){
        StringBuffer sb = new StringBuffer("");
        sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
        sb.append("<soap:Envelope "
                + "xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' "
                + "xmlns:xsd='http://www.w3.org/2001/XMLSchema' "
                + "xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>");
        sb.append("<soap:Body>");
        sb.append("<" + methodName + " xmlns='http://cxf.zyh.com/'>");

        //拼接Xml
        sb.append("<Message>");
        sb.append("<head>");
        sb.append("<serialNO>");
        sb.append("1");
        sb.append("</serialNO>");
        sb.append("</head>");
        sb.append("</Message>");


        sb.append("</" + methodName + ">");
        sb.append("</soap:Body>");
        sb.append("</soap:Envelope>");

        return sb.toString();
    }



}
